﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WebApiTool
{
    /// <summary>
    /// 使用ajax来提交表单
    /// </summary>
    public class TestPageWriter
    {
        StringBuilder _stringBuilder = new StringBuilder();

        DScenario _scenario;
        DAction _action;

        public TestPageWriter(DScenario scenario, DAction action)
        {
            _scenario = scenario;
            _action = action;
        }

        public void Build(string filePath, string apiUrl)
        {
            Begin();

            BeginForm(apiUrl);
            OnBuildSysParameters();

            OnBuildOpParameters();

            EndForm();

            End();
            File.WriteAllText(filePath, _stringBuilder.ToString());
        }

        private void Begin()
        {
            _stringBuilder.AppendLine("<!DOCTYPE html>");
            _stringBuilder.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");

            OnBuildHead();

            _stringBuilder.AppendLine("<body>");
        }

        private void End()
        {
            _stringBuilder.AppendLine("</body>");
            _stringBuilder.AppendLine("</html>");
        }

        private void OnBuildHead()
        {
            _stringBuilder.AppendLine("<head>");
            _stringBuilder.AppendLine("<meta http-equiv=\"Cache\" content=\"no-cache\" />");
            _stringBuilder.AppendLine("<meta charset=\"utf-8\"/>");
            _stringBuilder.AppendFormat("<title>[{0}]-{1}</title>", _scenario.Name, _action.Name);
            _stringBuilder.AppendLine("<link href=\"../css/style.css\" rel=\"stylesheet\" />");
            _stringBuilder.AppendLine("</head>");
        }

        private void BeginForm(string apiUrl)
        {
            _stringBuilder.AppendFormat("<form action=\"{0}\" method=\"{1}\"  enctype=\"multipart/form-data\">", 
                apiUrl,
                _action.HttpMethod);

            _stringBuilder.AppendFormat("<h1>[{0}]-{1}</h1>", _scenario.Name, _action.Name);
        }

        private void EndForm()
        {
            _stringBuilder.AppendLine("<button type=\"submit\">submit</button>");

            _stringBuilder.AppendLine("</form>");
        }

        private void OnBuildSysParameters()
        {
            _stringBuilder.AppendLine("<label>sys:sno</label><br/>");
            _stringBuilder.AppendFormat("<input name=\"sys:sno\" value=\"{0}\" readonly=\"readonly\"/><br/>", _scenario.Name);
            _stringBuilder.AppendLine("<label>sys:act</label><br/>");
            _stringBuilder.AppendFormat("<input name=\"sys:act\" value=\"{0}\" readonly=\"readonly\"/><br/>", _action.Name);
            _stringBuilder.AppendLine("<hr/>");
        }

        private void OnBuildOpParameters()
        {
            foreach (var parameter in _action.Parameters)
            {
                _stringBuilder.AppendLine("<label>");
                _stringBuilder.AppendFormat("{0}", parameter.Name);
                _stringBuilder.AppendLine("</label>");
                _stringBuilder.AppendLine("<br/>");

                if (parameter.Type.FullName == "System.Web.HttpPostedFile")
                {
                    _stringBuilder.AppendFormat("<input name=\"{0}\" type=\"file\" />", parameter.Name);
                    _stringBuilder.AppendLine("<br/>");
                }
                else
                {
                    _stringBuilder.AppendFormat("<input name=\"{0}\" value=\"{1}\" />", parameter.Name, string.Empty);
                    _stringBuilder.AppendLine("<br/>");
                }

                if (!string.IsNullOrEmpty(parameter.Description))
                {
                    _stringBuilder.AppendLine("<em>");
                    _stringBuilder.AppendFormat("{0}", parameter.Description);
                    _stringBuilder.AppendLine("</em>");
                    _stringBuilder.AppendLine("</br>");
                }
            }
        }

    }
}
